<?php $this->load->view('header'); ?>

<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>application/resources/js/ext_plugins/statusbar/css/statusbar.css"/>
<script type="text/javascript" src="<?php echo base_url(); ?>application/resources/js/ext_plugins/statusbar/StatusBar.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>application/resources/js/application/form_validation.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>application/resources/js/jshash-2.2/md5-min.js"></script>
    

<script type="text/javascript">
Ext.onReady(function() {

    // Form Login
    var formLogin = new Ext.FormPanel({
			frame: false, 
			border: false, 
			buttonAlign: 'center',
			url: BASE_URL + 'users/authenticate', 
			method: 'POST', 
			id: 'frmLogin',
			bodyStyle: 'padding:10px 10px 15px 15px;background:#dfe8f6;',
			width: 360, 
			labelWidth: 110,
			items: [{
				xtype: 'textfield',
				fieldLabel: 'Nombre de usuario',
				name: 'username',
				id: 'logUsername',
				allowBlank: false,
				width: 190,
				msgTarget: 'side'
			}, {
				xtype: 'textfield',
				fieldLabel: 'Contrase&ntilde;a',
				name: 'password',
	      id: 'logPassword',
				allowBlank: false,
				width: 190,
				msgTarget: 'side',
				inputType: 'password',
				listeners: {
					specialkey: function(f, e ) {
						if (e.getKey() == e.ENTER) {
							fnLogin();
						}
					}
				}
			}, {
				xtype: 'hidden',
				value: '<?php echo $this->session->userdata('session_id');?>',
				id: 'hiddenSeed'
			}, {
					xtype: 'hidden',
					id: 'encrytedPassword',
					name: 'password'
				}
			],

			buttons: [
				{ text: 'Login', handler: fnLogin },
				{ text: 'Reset', handler: function() {
						formLogin.getForm().reset();
					}
				}
			]
		});

    function fnLogin() {
    	formLogin.getForm().on('beforeaction', 
            function(form, action) {
                if (formLogin.getForm().isValid() && action.type == 'submit') {
                    var passwordTextField = Ext.getCmp('logPassword');
                		var hash = hex_md5( Ext.getCmp('hiddenSeed').value + hex_md5(passwordTextField.getValue()) );
                		var encrytedPassword = Ext.getCmp('encrytedPassword');
                		if(encrytedPassword.getValue != hash) {
                			encrytedPassword.setValue(hash);
                		}
                    Ext.getCmp('winLogin').body.mask();
                    Ext.getCmp('sbWinLogin').showBusy();
                }
        }, this);
        
      formLogin.getForm().submit({
           success: function() {
               window.location = BASE_URL;
           },
           failure: function(form, action) {
               Ext.getCmp('winLogin').body.unmask();
               if (action.failureType == 'server') {
                    obj = Ext.util.JSON.decode(action.response.responseText);
                    Ext.getCmp('sbWinLogin').setStatus({
                        text: obj.errors.reason,
                        iconCls: 'x-status-error'
                    });
                } else {
                    if (formLogin.getForm().isValid()) {
                        Ext.getCmp('sbWinLogin').setStatus({
                            text: 'Servidor de autenticaci&oacute;n inalcanzable.',
                            iconCls: 'x-status-error'
                        });
                    } else {
                        Ext.getCmp('sbWinLogin').setStatus({
                            text: 'Algun error en el formulario!',
                            iconCls: 'x-status-error'
                        });
                    }
                }
           }
       });
    }

	var winLogin = new Ext.Window({
		title: 'SISMAPC &mdash; Iniciar sesi&oacute;n',
    id: 'winLogin',
		layout: 'fit',
		width: 355,
		height: 160,
		resizable: false,
		closable: false,
		items: [formLogin],
    bbar: new Ext.ux.StatusBar({
        text: '&nbsp;',
        id: 'sbWinLogin',
        busyText: 'Chequeando contrase&ntilde;a...'
    }),
    listeners: {
		    show: function(w) {
		    	var tf = Ext.getCmp('logUsername');
		    	tf.getEl().highlight();
				}
		}
	});

	winLogin.show();
	winLogin.center();
});

</script>

<?php $this->load->view('footer'); ?>